package gestion.dao;

import gestion.conexion.ConexionBD;
import gestion.excepcion.DAOExcepcion;
import gestion.modelo.CentroFormacion;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class CentroFormacionDAO extends BaseDAO {

	public CentroFormacion Insertar (CentroFormacion cf){
		String query = "insert into centro_informacion (nombre,url,logo,tipo_idtipo) values (?,?,?,?) ";
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try {
			conn = ConexionBD.obtenerConexion();
			pstm = conn.prepareStatement(query);
			pstm.setString(1, cf.getNombre());
			pstm.setString(2, cf.getUrl());
			pstm.setString(3, cf.getLogo());
			pstm.setInt(4, cf.getTipoCentro());
			
		int i = pstm.executeUpdate();	
		
			
			
		} catch (SQLException e) {
	System.out.println(e.getMessage());
	
		}finally{
			this.cerrarConexion(conn);
			this.cerrarResultSet(rs);
			this.cerrarStatement(pstm);
			
					
		}
		
		return cf;
	}


	public CentroFormacion Modificar( CentroFormacion cf){
		
		String query = "update centro_informacion set nombre=?, url=?,logo=?, tipo_idtipo=1 where " +
				"idcentro_informacion = ? ";
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try {
			conn= ConexionBD.obtenerConexion();
			pstm = conn.prepareStatement(query);
			pstm.setString(1, cf.getNombre());
			pstm.setString(2, cf.getUrl());
			pstm.setString(3, cf.getLogo());
			pstm.setInt(4, cf.getId());
			
		pstm.executeUpdate();
		
			
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}finally{
			this.cerrarConexion(conn);
			this.cerrarResultSet(rs);
			this.cerrarStatement(pstm);
			
		}
		
		
		
		
		return cf;
	}
	
	public Collection< CentroFormacion> listarCentroFormacion() throws DAOExcepcion {
		Collection<CentroFormacion> lista = new ArrayList<CentroFormacion>();
		
		String query = "select idcentro_informacion, nombre, url,logo,pago  from centro_informacion";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				CentroFormacion vo = new CentroFormacion();
				vo.setId(rs.getInt("idcentro_informacion"));
				vo.setNombre(rs.getString("nombre"));
				vo.setUrl(rs.getString("url"));
				vo.setLogo(rs.getString("logo"));
			//	vo.setMontoCobrar(rs.getDouble("pago"));	
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println("cantidad de centros :"+lista.size());
		
		return lista;	
	}
	
	
	public ArrayList<CentroFormacion> Buscar(String nombre, int tipo) {
		String nom = null;
		String tip = null;
		// String xx = "nombre like ? or";

		if (nombre.isEmpty() == true) {
			nom = "";
			tip = "tipo_idtipo like ?";
		} else {
			nom = "nombre like ?";
			tip = "";
		}
		String query = "Select idcentro_informacion, nombre, url, tipo_idtipo from centro_informacion "
				+ "where " + nom + " " + tip + " ";

		ArrayList<CentroFormacion> listar = new ArrayList<CentroFormacion>();
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		try {
			conn = ConexionBD.obtenerConexion();
			pstm = conn.prepareStatement(query);
			if (nom.length() >= 1) {
				pstm.setString(1, "%" + nombre + "%");
				//pstm.setInt(2, tipo);
			} else {
				pstm.setInt(1, tipo);
			}
			rs = pstm.executeQuery();
			while (rs.next()) {
				CentroFormacion centros = new CentroFormacion();
				centros.setId(rs.getInt(1));
				centros.setNombre(rs.getString(2));
				centros.setUrl(rs.getString(3));
				centros.setTipoCentro(rs.getInt(4));
				listar.add(centros);

			}

		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			this.cerrarConexion(conn);
			this.cerrarResultSet(rs);
			this.cerrarStatement(pstm);

		}
		System.out.println(listar.size());
		return listar;

	}
	
	
public void Eliminar( int Id){
		
		String query = "Delete centro_informacion  where " +
				"idcentro_informacion = ? ";
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try {
			conn= ConexionBD.obtenerConexion();
			pstm = conn.prepareStatement(query);
			pstm.setInt(1, Id);
			
		pstm.executeUpdate();
		
			
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}finally{
			this.cerrarConexion(conn);
			this.cerrarResultSet(rs);
			this.cerrarStatement(pstm);
			
		}
		
	}
	
	
	

}
	
	
	
	
	
	
	
	
	
	
	
	